package com.mes.web.controller.biz;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.page.PageDomain;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.mes.system.strategy.AutoCodeUtil;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizCustomer;
import com.mes.biz.service.BizCustomerService;

/**
 * 客户档案Controller
 * @author hwg
 * @email hwg
 * @date 2023-04-30
 */
@RestController
@RequestMapping("/biz/bizCustomer")
public class BizCustomerController extends BaseController {

    @Autowired
    private BizCustomerService bizCustomerService;
    @Resource
    private AutoCodeUtil autoCodeUtil;

    /**
     * 查询客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizCustomer bizCustomer, HttpServletRequest request, HttpServletResponse response) {
        bizCustomer.setPage(new PageDomain(request, response));
        return R.data(bizCustomerService.findPage(bizCustomer));
    }

    /**
     * 查询客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:list')")
    @GetMapping("/allList")
    public R<List<BizCustomer>> allList(BizCustomer bizCustomer) {
        return R.data(bizCustomerService.findList(bizCustomer));
    }

    /**
     * 获取客户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:query')")
    @GetMapping(value = "/{id}")
    public R<BizCustomer> detail(@PathVariable("id") String id) {
        return R.data(bizCustomerService.get(id));
    }

    /**
     * 新增客户档案
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:add')")
    @Log(title = "客户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizCustomer bizCustomer) {
        bizCustomer.setTenantId(SecurityUtils.getTenantId());
        String customerCode  = autoCodeUtil.genSerialCode("CUSTOMER_CODE",null);
        bizCustomer.setCustomerCode(customerCode);
//        bizCustomerService.save(bizCustomer);
//        if (StringUtils.isNotEmpty(bizCustomer.getPhone())){
//            SysUser user = new SysUser();
//            user.setTenantId(SecurityUtils.getTenantId());
//            user.setPhonenumber(bizCustomer.getPhone());
//            user.setStatus("0");
//            user.setDelFlag("0");
//            userService.save(user);
//        }

        return R.status(bizCustomerService.save(bizCustomer));
    }

    /**
     * 修改客户档案
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:edit')")
    @Log(title = "客户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizCustomer bizCustomer) {
        return R.status(bizCustomerService.save(bizCustomer));
    }

    /**
     * 删除客户档案
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:remove')")
    @Log(title = "客户档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizCustomerService.deleteBizCustomerByIds(ids));
    }


    /**
     * 校验客户名称的唯一性
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:query')")
    @Log(title = "客户档案", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/checkCustomerNameUnique/{customerName}/{id}", "/checkCustomerNameUnique/{customerName}"})
    public R checkCustomerNameUnique(@NotBlank(message = "客户名称不允许为空") @PathVariable("customerName") String customerName, @PathVariable(value = "id", required = false) String id) {
        BizCustomer bizCustomer = new BizCustomer();
        bizCustomer.setCustomerName(customerName);
        bizCustomer.setId(id);
        bizCustomerService.checkCustomerNameUnique(bizCustomer);
        return R.status(Boolean.TRUE);
    }

    /**
     * 导出客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:export')")
    @Log(title = "客户档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizCustomer bizCustomer) {
        List<BizCustomer> list = bizCustomerService.findList(bizCustomer);
        ExcelUtil<BizCustomer> util = new ExcelUtil<BizCustomer>(BizCustomer.class);
        return util.exportExcel(list, "客户档案数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
